#use "topfind";;
#load "str.cma";;
#require "netclient";;
#require "netstring";;

open Http_client.Convenience;;
module StringMap = Map.Make (String);;

(* takes a url and returns the HtmlDoc representation of it *)
let parse url = Nethtml.parse_document (Lexing.from_string (http_get url));;


(* returns only the tags from the HtmlDoc that or of type tag *)
let getArrayByTag tag docList = 
  let rec find tag = function
    Nethtml.Element(content, a, c) ->
      if (tag = content) then [a] else List.concat(List.map (find tag) c)
  | Nethtml.Data(e) -> [] in
    let getListByTag tag docList = (List.concat (List.map (find tag) docList)) in
      let makeMap tupleList = 
        List.fold_left (fun mappy (a,b) -> print_string(a ^ " " ^ b ^ "\n"); StringMap.add a b mappy) (StringMap.empty) tupleList in
          let makeAttributeMap attributeTuples = List.map makeMap attributeTuples in
            makeAttributeMap (getListByTag tag docList);;


(* get length of a list *)
let getLength thelist = List.length thelist;;


(* filter list of tags by attribute and value *)
let filterArrayByAttribute attr value taglist =
  let filterByAttribute attr value attributeMap =
    if (StringMap.mem attr attributeMap)
      then if ((StringMap.find attr attributeMap) = value)
        then [attributeMap]
        else []
      else [] in
  List.fold_left (fun retList tag -> List.append retList (filterByAttribute attr value tag)) [] taglist;;



